// packageE/appointment/calender/calender.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    calendarEvents: {
      type: Array,
      value: [],
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    preDateList: [],
    currentDateList: [],
    nextDateList: [],
    dataList: [],
    swiperCurrent: 0,
    year: "",
    month: "",
    currentDate: "",
    currentMonth:
      getDateObj().getFullYear() + "-" + (getDateObj().getMonth() + 1),
    current_date_id: getDateObj().getDate() - 1 + get_currentMonthDay(),
    selected_data_id: getDateObj().getDate() - 1 + get_currentMonthDay(),
  },

  ready() {
    let dateObj = new Date();
    let month = dateObj.getMonth() + 1;
    let selected_data_id =
      dateObj.getFullYear() +
      "-" +
      (month <= 9 ? "0" + month : month) +
      "-" +
      (dateObj.getDate() <= 9 ? "0" + dateObj.getDate() : dateObj.getDate());
    this.setData({
      current_date_id:
        dateObj.getFullYear() +
        "-" +
        (month <= 9 ? "0" + month : month) +
        "-" +
        (dateObj.getDate() <= 9 ? "0" + dateObj.getDate() : dateObj.getDate()),
      selected_data_id,
    });
    this.triggerEvent("select", {
      work_date: selected_data_id,
      disabled: this.data.calendarEvents.includes(selected_data_id),
    });
    this.setCurrentMonth("current");
    this.setPreMonth("pre");

    this.setNextMonth("next");

    this.setData({
      dataList: [
        this.data.currentDateList,
        this.data.nextDateList,
        this.data.preDateList,
      ],
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    changeDate(e) {
      this.setData({
        selected_data_id: e.currentTarget.dataset.index,
      });
      let disabled = e.currentTarget.dataset.disabled || false;
      this.triggerEvent("select", {
        work_date: e.currentTarget.dataset.index,
        disabled: disabled,
      });
    },
    nextMonth() {
      let oldValue = this.data.swiperCurrent;
      let newValue =
        this.data.swiperCurrent == 2 ? 0 : this.data.swiperCurrent + 1;
      this.setData({
        swiperCurrent: newValue,
      });

      this.changeMonth(oldValue, newValue);
    },
    preMonth() {
      let oldValue = this.data.swiperCurrent;
      let newValue =
        this.data.swiperCurrent == 0 ? 2 : this.data.swiperCurrent - 1;

      this.setData({
        swiperCurrent: newValue,
      });
      this.changeMonth(oldValue, newValue);
    },
    changeMonth(oldValue, newValue) {
      if (newValue == 0 && oldValue == 2) {
        if (this.data.month == 11) {
          this.data.month = 0;
          this.data.year = this.data.year + 1;
        } else {
          this.data.month = this.data.month + 1;
        }
      } else if (newValue == 2 && oldValue == 0) {
        if (this.data.month == 0) {
          this.data.month = 11;
          this.data.year = this.data.year - 1;
        } else {
          this.data.month = this.data.month - 1;
        }
      } else if (oldValue > newValue) {
        if (this.data.month == 0) {
          this.data.month = 11;
          this.data.year = this.data.year - 1;
        } else {
          this.data.month = this.data.month - 1;
        }
      } else if (oldValue < newValue) {
        if (this.data.month == 11) {
          this.data.month = 0;
          this.data.year = this.data.year + 1;
        } else {
          this.data.month = this.data.month + 1;
        }
      }

      let pre = newValue == 0 ? 2 : newValue - 1;
      let next = newValue == 2 ? 0 : newValue + 1;
      let current = newValue;
      this.setIndexMonth("current");
      this.setNextMonth("next");
      this.setPreMonth("prev");
      let data = [];
      data[pre] = this.data.preDateList;
      data[next] = this.data.nextDateList;
      data[current] = this.data.currentDateList;

      this.setData({
        dataList: data,
      });
    },
    swiperChangeMonth(e) {
      console.log(this.data.swiperCurrent);
      console.log(e.detail.current);
      let index = e.detail.current;
      this.changeMonth(this.data.swiperCurrent, index);
      this.data.swiperCurrent = index;
    },
    setCurrentMonth(monthType) {
      var dateObj = getDateObj();
      this.setData({
        //currentDate: dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate()+'日'
      });
      let currentDateList = getDateList(dateObj, this, monthType);

      forDateList(this.data.calendarEvents, currentDateList);
      this.setData({
        currentDateList: currentDateList.array,
      });
    },
    setIndexMonth(monthType) {
      var dateObj = getDateObj();
      dateObj.setMonth(this.data.month); //获取当前月份
      dateObj.setFullYear(this.data.year); //获取当前年份
      this.setData({
        //currentDate: dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate()+'日'
      });
      let currentDateList = getDateList(dateObj, this, monthType);

      forDateList(this.data.calendarEvents, currentDateList);
      console.log(currentDateList, this.data.calendarEvents);
      this.setData({
        currentDateList: currentDateList.array,
      });
    },
    setPreMonth(monthType) {
      console.log(this.data);
      let obj = new Date();
      if (this.data.month == 0) {
        obj.setMonth(11); //获取当前月份
        obj.setFullYear(this.data.year - 1); //获取当前年份
      } else {
        obj.setMonth(this.data.month - 1); //获取当前月份
        obj.setFullYear(this.data.year); //获取当前年份
      }
      let preDateList = getDateList(obj, this, monthType);
      this.data.preDateList = preDateList.array;
    },
    setNextMonth(monthType) {
      let obj = new Date();
      if (this.data.month == 11) {
        obj.setMonth(0); //获取当前月份
        obj.setFullYear(this.data.year + 1); //获取当前年份
      } else {
        obj.setMonth(this.data.month + 1); //获取当前月份
        obj.setFullYear(this.data.year); //获取当前年份
      }
      let nextDateList = getDateList(obj, this, monthType);
      this.data.nextDateList = nextDateList.array;
    },
  },
});

function forDateList(res, date_list) {
  for (var i = 0, j = res.length; i < j; i++) {
    for (var k = 0, l = date_list.array.length; k < l; k++) {
      let date = `${res[i].split('-')[0]}-${res[i].split('-')[1]}-${res[i].split('-')[2]}`;
      if (date == date_list.array[k].work_date) {
        date_list.array[k].disabled = true;
        if(res[i].split('-')[3]){
          date_list.array[k].name = res[i].split('-')[3];
        }
      }
    }
  }
}
forDateList.judge = function (res, date_list, _this) {
  if (res.length == 0) {
    _this.setData({
      currentDateList: date_list.array,
      detail_content: "本月未写日记",
      off_time: "",
      detail_id: 0,
    });
  } else {
    _this.setData({
      currentDateList: date_list.array,
      detail_content:
        date_list.array[_this.data.date_id].content == undefined
          ? "当天没写日记"
          : date_list.array[_this.data.date_id].content,
      off_time:
        date_list.array[_this.data.date_id].off_time == undefined
          ? ""
          : date_list.array[_this.data.date_id].off_time,
      detail_id:
        date_list.array[_this.data.date_id].id == undefined
          ? 0
          : date_list.array[_this.data.date_id].id,
    });
  }
};
function getDateObj() {
  // var date = new Date();
  // var a =
  //   date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
  return new Date();
}

function getDateList(obj, _this, monthType) {
  var array = [];
  // var date = obj.getDate();
  obj.setDate(1); // 设置日期为1,
  var Dday = obj.getDay(); //获取当月第一天是星期几
  var Dmonth = obj.getMonth(); //获取当前月份
  var Dyear = obj.getFullYear(); //获取当前年份
  var date_num = getDateNum(Dmonth + 1, Dyear);
  var prev = get_prev_month({
    month: obj.getMonth(),
    year: obj.getFullYear(),
  });

  var next = get_next_month({
    month: obj.getMonth(),
    year: obj.getFullYear(),
  });

  if (Dday == 0) {
    //本月第一天为星期天
    var num = 42 - date_num;
    prev.month += 1;
    for (let i = 0; i < date_num; i++) {
      let date = i < 9 ? "0" + (i + 1) : i + 1;
      let month = Dmonth < 9 ? "0" + (Dmonth + 1) : Dmonth + 1;
      array.push({
        type: "current",
        date,
        month,
        year: Dyear,
        work_date: Dyear + "-" + month + "-" + date,
        overdue: be_overdue(Dyear, Dmonth + 1, i + 1),
      });
    }
    for (let i = 0; i < num; i++) {
      let date = i < 9 ? "0" + (i + 1) : i + 1;
      let month = next.month <= 9 ? "0" + next.month : next.month;
      array.push({
        type: "next",
        date,
        month,
        year: next.year,
        work_date: next.year + "-" + month + "-" + date,
        overdue: be_overdue(next.year, next.month, i + 1),
      });
    }
  } else {
    for (let i = prev.date - Dday + 1; i < prev.date + 1; i++) {
      let date = i;
      let month = prev.month <= 9 ? "0" + prev.month : prev.month;
      array.push({
        type: "prev",
        date,
        month,
        year: prev.year,
        work_date: prev.year + "-" + month + "-" + date,
        overdue: be_overdue(prev.year, prev.month, i),
      });
    }
    for (let i = 0; i < date_num; i++) {
      let date = i < 9 ? "0" + (i + 1) : i + 1;
      let month = Dmonth < 9 ? "0" + (Dmonth + 1) : Dmonth + 1;
      array.push({
        type: "current",
        date,
        month,
        year: Dyear,
        work_date: Dyear + "-" + month + "-" + date,
        overdue: be_overdue(Dyear, Dmonth + 1, i + 1),
      });
    }
    for (var i = 0, j = 42 - array.length; i < j; i++) {
      let date = i < 9 ? "0" + (i + 1) : i + 1;
      let month = next.month <= 9 ? "0" + next.month : next.month;
      array.push({
        type: "next",
        date,
        month,
        year: next.year,
        work_date: next.year + "-" + month + "-" + date,
        overdue: be_overdue(next.year, next.month, i + 1),
      });
    }
  }

  if (monthType == "current") {
    _this.setData({
      currentDateList: array,
      year: Dyear,
      month: Dmonth,
      currentDate: Dyear + "-" + (Dmonth + 1),
    });
  }

  return {
    array: array,
    prev:
      prev.year +
      "-" +
      (prev.month <= 9 ? "0" + prev.month : prev.month) +
      "-" +
      array[0].date,
    next:
      next.year +
      "-" +
      (next.month <= 9 ? "0" + next.month : next.month) +
      "-" +
      array[41].date,
  };
}

function getDateNum(month, year) {
  var day30 = [4, 6, 9, 11];
  var day31 = [1, 3, 5, 7, 8, 10, 12];
  var month_num = 0; //设置当前月份的默认天数
  if (inArray(day30, month)) {
    //月份为30天
    month_num = 30;
  } else if (inArray(day31, month)) {
    //月份为31天
    month_num = 31;
  } else {
    //2月份
    if (parseInt(year) % 4 == 0 || parseInt(year) % 400 == 0) {
      //29天
      month_num = 29;
    } else {
      //28天
      month_num = 28;
    }
  }
  return month_num;
}

function get_prev_month(obj) {
  var date = new Date();
  let Dday = null;
  if (obj.month == 0) {
    date.setFullYear(obj.year - 1);
    date.setMonth(11);
    date.setDate(getDateNum(12, obj.year - 1));
    Dday = date.getDay();
    return {
      month: 12,
      year: obj.year - 1,
      day: Dday,
      date: getDateNum(12, obj.year - 1),
    };
  } else {
    date.setFullYear(obj.year);
    date.setMonth(obj.month - 1);
    date.setDate(getDateNum(obj.month, obj.year));
    Dday = date.getDay();
    return {
      month: obj.month,
      year: obj.year,
      day: Dday,
      date: getDateNum(obj.month, obj.year),
    };
  }
}

//获取本月的第一天是星期几
function get_currentMonthDay() {
  var date = new Date();
  date.setDate(1);
  return date.getDay();
}

function get_next_month(obj) {
  var date = new Date();
  let Dday = null;
  if (obj.month == 11) {
    date.setFullYear(obj.year + 1);
    date.setMonth(0);
    date.setDate(1);
    Dday = date.getDay();
    return {
      month: 1,
      year: obj.year + 1,
      day: Dday,
      date: 1,
    };
  } else {
    date.setFullYear(obj.year);
    date.setMonth(obj.month + 1);
    date.setDate(1);
    Dday = date.getDay();
    return {
      month: obj.month + 2,
      year: obj.year,
      day: Dday,
      date: 1,
    };
  }
}

function be_overdue(year, month, day) {
  var date = new Date();
  if (arguments.length == 3) {
    if (year < date.getFullYear()) {
      return "once";
    } else if (year == date.getFullYear()) {
      if (month < date.getMonth() + 1) {
        return "once";
      } else if (month == date.getMonth() + 1) {
        if (day <= date.getDate()) {
          return "once";
        } else {
          return "futrue";
        }
      } else {
        return "futrue";
      }
    } else {
      return "futrue";
    }
  } else if (arguments.length == 2) {
    if (year < date.getFullYear()) {
      return "once";
    } else if (year == date.getFullYear()) {
      if (month <= date.getMonth() + 1) {
        return "once";
      } else {
        return "futrue";
      }
    } else {
      return "futrue";
    }
  }
}

function inArray(array, value) {
  for (var i in array) {
    if (array[i] === value) return true;
  }
  return false;
}
